using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using framework;

namespace MvcExtExample.Handlers
{
    class logMasterAction
    {
        /// <summary>
        /// 获取日志列表
        /// </summary>
        /// <param name="strSort"></param>
        /// <param name="strField"></param>
        /// <param name="intStart"></param>
        /// <param name="intLimit"></param>
        /// <returns></returns>
        public string getLogList(string strSort, string strField, int intStart, int intLimit)
        {
            SqlConnection ts = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ToString());
            ts.Open();
            String strSqlStr = "SELECT TOP 20 [ID],[OperationType],[TableName],[BusinessesName],[Content],[User_ID],[CreateTime],[IP],[Location],[Browser]FROM [Oil_Water_System].[dbo].[Tb_LogOperation]";
            SqlCommand cop = new SqlCommand(strSqlStr, ts);
            SqlDataAdapter sda = new SqlDataAdapter(cop);
            DataSet dsResult = new DataSet();
            sda.Fill(dsResult);
            DataRowView rowview = dsResult.Tables[0].DefaultView[0];    //定义一个视图
            string Privilege = rowview[0].ToString();
            JSONHelper jsonHelp = new JSONHelper();
            jsonHelp.success = true;
            jsonHelp.totlalCount = getLogListCount();
            foreach (DataRow dr in dsResult.Tables[0].Rows)
            {
                jsonHelp.AddItem("ID", dr["ID"].ToString());
                jsonHelp.AddItem("User_ID", dr["User_ID"].ToString());
                jsonHelp.AddItem("OperationType", dr["OperationType"].ToString());
                jsonHelp.AddItem("CreateTime", Convert.ToDateTime(dr["CreateTime"]).ToString("yyyy/MM/dd"));
                jsonHelp.AddItem("IP", dr["IP"].ToString());
                if (dr["TableName"] != DBNull.Value)
                {
                    jsonHelp.AddItem("TableName", dr["TableName"].ToString());
                }
                else
                {
                    jsonHelp.AddItem("PRJ_STARTDATE", string.Empty);
                }
                if (dr["BusinessesName"] != DBNull.Value)
                {
                    jsonHelp.AddItem("BusinessesName", dr["BusinessesName"].ToString());
                }
                else
                {
                    jsonHelp.AddItem("BusinessesName", string.Empty);
                }
                if (dr["Content"] != DBNull.Value)
                {
                    jsonHelp.AddItem("Content", dr["Content"].ToString());
                }
                else
                {
                    jsonHelp.AddItem("Content", string.Empty);
                }
                if (dr["Location"] != DBNull.Value)
                {
                    jsonHelp.AddItem("Location", dr["Location"].ToString());
                }
                else
                {
                    jsonHelp.AddItem("Location", string.Empty);
                }
                if (dr["Browser"] != DBNull.Value)
                {
                    jsonHelp.AddItem("Browser", dr["Browser"].ToString());
                }
                else
                {
                    jsonHelp.AddItem("Browser", string.Empty);
                }
                jsonHelp.ItemOk();
            }
            string strResult = jsonHelp.ToString();
            return strResult;
        }

        /// <summary>
        /// 获取日志条数总数
        /// </summary>
        /// <returns></returns>
        private int getLogListCount()
        {
            SqlConnection ts = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ToString());
            ts.Open();
            string strSqlStr = "SELECT COUNT(*) FROM [dbo].[Tb_LogOperation]";
            SqlCommand cop = new SqlCommand(strSqlStr, ts);
            SqlDataAdapter sda = new SqlDataAdapter(cop);
            DataSet dsResult = new DataSet();
            sda.Fill(dsResult);
            DataRowView rowview = dsResult.Tables[0].DefaultView[0];    //定义一个视图
            return int.Parse(dsResult.Tables[0].Rows[0][0].ToString());
        }
    }
}
